﻿using System;
using System.Linq;
using System.Data.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using OpenGiving;

public partial class VolunteerEventDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        (Master.FindControl("PageSubtitleLabel") as Label).Text = "Event Details";
    }

    public int GetVolunteerCount(object currentItem)
    {
        Events evt = Page.GetUnderlyingEntity<Events>(currentItem);
        return evt.EventRegistrations.Count;
    }

    public string GetVolunteerButtonLabel(object currentItem)
    {
        Events evt = Page.GetUnderlyingEntity<Events>(currentItem);

        if (evt.EventRegistrations.Where(x => x.contact_id == Profile.OpenGivingID).Count() > 0)
            return "Cancel";
        else
            return "Volunteer";
    }

    protected void cbShowedUp_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbShowedUp = sender as CheckBox;
        HiddenField hf = cbShowedUp.Parent.FindControl("hfEventInfo") as HiddenField;

        // Only admins allowed here!
        if (User.IsInRole("Admin"))
        {
            using (OpenGivingDataContext db = new OpenGivingDataContext())
            {
                try
                {
                    string[] args = hf.Value.Split(':');
                    int user_id = Int32.Parse(args[0]);
                    int event_id = Int32.Parse(args[1]);

                    // Remove volunteer
                    EventRegistrations reg = db.EventRegistrations.Where(x => x.event_id == event_id && x.contact_id == user_id).FirstOrDefault();
                    reg.attended = cbShowedUp.Checked;

                    db.SubmitChanges();

                    ListViewEvent.DataBind();

                    Control c = LoginViewVolunteers.FindControl("rptVolunteers");
                    if (c != null) c.DataBind();
                }
                catch
                {
                    // TODO: Present error message somewhere
                }
                finally { }
            }
        }
    }

    protected void btnRemoveVolunteer_Click(object sender, EventArgs e)
    {
        // Only admins allowed here!
        if (User.IsInRole("Admin"))
        {
            using (OpenGivingDataContext db = new OpenGivingDataContext())
            {
                try
                {
                    Button btn = sender as Button;
                    string[] args = btn.CommandArgument.Split(':');
                    int user_id = Int32.Parse(args[0]);
                    int event_id = Int32.Parse(args[1]);

                    // Remove volunteer
                    EventRegistrations reg = db.EventRegistrations.Where(x => x.event_id == event_id && x.contact_id == user_id).FirstOrDefault();
                    //reg.dateUnregistered = DateTime.Now;
                    db.EventRegistrations.DeleteOnSubmit(reg);
                    
                    db.SubmitChanges();

                    ListViewEvent.DataBind();

                    Control c = LoginViewVolunteers.FindControl("rptVolunteers");
                    if (c != null) c.DataBind();
                }
                catch
                {
                    // TODO: Present error message somewhere
                }
                finally { }
            }
        }
    }

    protected void VolunteerButton_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        int event_id = int.Parse(btn.CommandArgument);

        using (OpenGivingDataContext db = new OpenGivingDataContext())
        {
            using (TraceTextWriter traceWriter = new TraceTextWriter())
            {
                try
                {
                    if (btn.Text == "Volunteer")
                    {
                        // Volunteer
                        EventRegistrations reg = new EventRegistrations();
                        reg.Events = db.Events.Where(x=>x.event_id == event_id).FirstOrDefault();
                        reg.UserMappings = db.UserMappings.Where(x=>x.contact_id == Profile.OpenGivingID).FirstOrDefault();
                        reg.dateRegistered = DateTime.Now;
                        //TODO: Let user select organization here?
                        reg.organization_id = 1;

                        db.EventRegistrations.InsertOnSubmit(reg);
                    }
                    else
                    {
                        // Remove volunteer
                        EventRegistrations reg = db.EventRegistrations.Where(x => x.event_id == event_id && x.contact_id == Profile.OpenGivingID).FirstOrDefault();
                        //reg.dateUnregistered = DateTime.Now;
                        db.EventRegistrations.DeleteOnSubmit(reg);
                    }

                    db.SubmitChanges();

                    ListViewEvent.DataBind();

                    if (User.IsInRole("Admin"))
                    {
                        Control c = LoginViewVolunteers.FindControl("rptVolunteers");
                        if( c!= null ) c.DataBind();
                    }
                }
                finally { }
            }
        }
    }
}
